import { Component, Injector, ViewEncapsulation } from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import { PipelineListDto } from "@shared/service-proxies/service-proxies";
import { PipelineNodeServiceProxy } from "@shared/service-proxies/service-proxies";
import { appModuleAnimation } from "@shared/animations/routerTransition";

@Component({
  selector: "rw3",
  templateUrl: "./rw3.component.html",
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()],
})
export class RW3ModalComponent extends AppComponentBase {
  saving = false;

  pipe: PipelineListDto = new PipelineListDto();

  tiaoMaCode = "";

  constructor(
    injector: Injector,
    private _taskServiceProxy: PipelineNodeServiceProxy
  ) {
    super(injector);
  }

  load(pipe: PipelineListDto): void {
    this.pipe = pipe;

    this._taskServiceProxy.getTiaoMaCode(pipe.id).subscribe((result) => {
      this.tiaoMaCode = result;
    });
  }

  save(): void {
    this.saving = true;

    let evt = document.createEvent("CustomEvent");
    let str =
      this.pipe.taskName +
      "," +
      this.pipe.taskCode +
      "," +
      this.pipe.amountPlanned +
      "," +
      this.tiaoMaCode;
    evt.initCustomEvent("myCustomEvent", true, false, encodeURI(str));

    // fire the event
    document.dispatchEvent(evt);

    this.saving = false;
  }
}
